  set more off

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                      PROCESING EXPERIMENT DATA
*====================================================================*



*====================================================================*
* Checking how different are the datasets of Provider Listing
*====================================================================*

* Eliminates missing values for uid
  use "$rawdatap2\Deidentified_ProviderData_15July2015", clear
  drop if cluster==.
  egen tag=tag(cluster uid)
  bys cluster: egen x=rank(tag), unique
  replace uid=cluster + 999999 + x if uid==. & cluster!=.
  tempfile Deidentified
  save "`Deidentified'"

* Open Data
  use "$rawdatap2\DeidentifiedProviderListingData_270315", clear
  count
  gen uid = UID
  merge 1:1 uid using "`Deidentified'", keepus(cluster provider_type_string provider_type)

* Matched
/*NOTE: no difference in classification*/
  compare provider_type Provider_Type if _merge==3

* Not matched
  tab Cluster_number if _merge==1
  tab  cluster if _merge==2

  list uid Cluster_number Provider_Type if Cluster_number==11 & _merge==1
  list uid cluster provider_type_string provider_type if cluster==11 & _merge==2

  list uid Cluster_number Provider_Type if Cluster_number==176 & _merge==1
  list uid cluster provider_type_string provider_type if cluster==176 & _merge==2


  list uid Cluster_number Provider_Type if Cluster_number==429 & _merge==1
  list uid cluster provider_type_string provider_type if cluster==429 & _merge==2


*====================================================================*
* # of WHP per cluster
*====================================================================*

* Open Data
  use "$rawdatap2\DeidentifiedProviderListingData_270315", clear
  count

* Correct clusters based on other WHP listing
  gen uid = UID
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1 cluster_id_endline)
  list uid if _merge==2
  replace Provider_Type = 3 if Provider_Type!=3 & _merge>1
  replace Cluster_number = cluster_id_endline if _merge==2
  drop _merge

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  list uid if Provider_Type!=3 & _merge==3
  replace Provider_Type = 3 if Provider_Type!=3 & _merge==3
  drop _merge 

* WHP Providers by cluster
  gen aux = 1 if Provider_Type==3
  bys Cluster_number: egen nwhp = sum(aux)
  lab var nwhp "Number of WHP providers in the cluster"
  egen tag=tag(Cluster_number)
  tab nwhp if tag

* Providers located in the field prior to the survey
  replace nwhp = 1 if Cluster_number == 14 & nwhp==0
  replace nwhp = 1 if Cluster_number == 59 & nwhp==0
  replace nwhp = 1 if Cluster_number == 148 & nwhp==0
  replace nwhp = 1 if Cluster_number == 227 & nwhp==0

* Providers missed during listing - found during post-survey verification
  replace nwhp = 1 if Cluster_number == 13 & nwhp==0
  replace nwhp = 1 if Cluster_number == 352 & nwhp==0

* Save
  keep if tag==1
  rename Cluster_number cluster
  keep cluster nwhp
  save "$prodata2\WHP_by_cluster", replace
